<template>
	<div class="node-wrap">
		<div class="node-wrap-box" @click="show">
			<div class="node-wrap-box-title" :style="{background: nodeConfig.color}">
				<i class="icon el-icon-s-promotion"></i>
				<span>{{ nodeConfig.nodeName }}</span>
				<i class="close el-icon-close" v-if="!disabled" @click.stop="delNode()"></i>
			</div>
			<div class="content">
				<!-- <span v-if="toText(nodeConfig)">{{ toText(nodeConfig) }}</span>
				<span v-else class="placeholder">请选择人员</span> -->
        <span v-if="nodeConfig.nodeDescription">{{nodeConfig.nodeDescription}}</span>
        <span v-else-if="toText(nodeConfig)">{{ toText(nodeConfig) }}</span>
        <span v-else class="placeholder">请选择人员</span>
        <img v-show="showCompleteIcon && nodeConfig.complete" class="node-pass-icon" src="../../../assets/pass.png" alt="">
			</div>
      <div class="wh-node-operation" v-if="form.dealButtonNames && form.dealButtonNames.length>0">
        <p class="wh-node-des">操作按钮：</p>
        <div class="wh-node-btns">
          <el-tag type="info" style="margin-right: 5px; margin-bottom: 5px;" size="mini" v-for="(item, index) in nodeConfig.dealButtonNames" :key="index">{{item}}</el-tag>
        </div>
      </div>
		</div>
		<add-node :parentNodeId="nodeConfig.nodeId" :base-url="baseUrl" :headers="headers" v-model="nodeConfig.childNode" :disabled="disabled"></add-node>
		<el-drawer title="抄送人设置" :visible.sync="drawer" destroy-on-close append-to-body :size="500">
			<template #title>
				<div class="node-wrap-drawer__title">
					<label @click="editTitle" v-if="!isEditTitle">{{form.nodeName}}<i class="node-wrap-drawer__title-edit el-icon-edit"></i></label>
					<el-input v-if="isEditTitle" ref="nodeTitle" v-model="form.nodeName" clearable @blur="saveTitle" @keyup.enter="saveTitle"></el-input>
				</div>
			</template>
			<el-container>
				<el-main style="padding:0 20px 20px 20px">
					<el-form label-position="top">
            <el-form-item label="节点描述">
            	<el-input v-model="form.nodeDescription" placeholder="请输入节点描述"></el-input>
            </el-form-item>
            <el-form-item label="卡片顶部颜色">
            	<el-color-picker v-model="form.color"></el-color-picker>
            </el-form-item>
            <el-form-item label="操作">
              <el-select v-model="form.dealButtons" multiple placeholder="请选择">
                <el-option
                  v-for="item in dealList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
						<el-form-item label="选择要抄送的人员">
							<el-button type="primary" icon="el-icon-plus" round @click="selectHandle(1, form.nodeUserList)">选择人员</el-button>
							<div class="tags-list">
								<el-tag v-for="(user, index) in form.nodeUserList" :key="user.id" closable @close="delUser(index)">{{user.name}}</el-tag>
							</div>
						</el-form-item>
						<el-form-item label="">
							<el-checkbox v-model="form.userSelectFlag" label="允许发起人自选抄送人"></el-checkbox>
						</el-form-item>
					</el-form>
				</el-main>
				<el-footer>
					<el-button type="primary" @click="save">保存</el-button>
					<el-button @click="cancel">取消</el-button>
				</el-footer>
			</el-container>
		</el-drawer>
	</div>
</template>

<script>
	import addNode from './addNode.vue'
  import {
    flowButtonList
  } from '../../../network/api.js'
	export default {
		inject: ['select'],
		props: {
			value: { type: Object, default: () => {} },
      disabled: {
        type: Boolean,
        default: false
      },
      baseUrl: {
        type: String,
        default: ''
      },
      headers: {
        type: Object,
        default: () => {
          return {}
        }
      },
      showCompleteIcon: {
        type: Boolean,
        default: false
      }
		},
		components: {
			addNode
		},
		data() {
			return {
				nodeConfig: {},
				drawer: false,
				isEditTitle: false,
				form: {},
        dealList: []
			}
		},
		watch:{
			value(){
				this.nodeConfig = this.value
			}
		},
		mounted() {
			this.nodeConfig = this.value
		},
		methods: {
			show(){
        if (this.disabled) return
				this.form = {}
				this.form = JSON.parse(JSON.stringify(this.nodeConfig))
				this.drawer = true
        this.initDealData()
			},
      initDealData () {
        flowButtonList(this.baseUrl, this.headers, {}).then(res => {
          console.log(res);
          if (res.code=== 200) {
            this.dealList = res.data
          }
        })
      },
			editTitle(){
				this.isEditTitle = true
				this.$nextTick(()=>{
					this.$refs.nodeTitle.focus()
				})
			},
			saveTitle(){
				this.isEditTitle = false
			},
			save(){
        var arr = []
        this.form.dealButtons.forEach(ele => {
          let obj = this.dealList.find(e => e.id === ele)
          if (obj) {
            arr.push(obj.name)
          }
        })
        this.form.dealButton = this.form.dealButtons.join(',')
        this.form.dealButtonNames = arr
				this.$emit("input", this.form)
				this.drawer = false
			},
      cancel () {
        this.drawer = false
      },
			delNode(){
				this.$emit("input", this.nodeConfig.childNode)
			},
			delUser(index){
				this.form.nodeUserList.splice(index, 1)
			},
			selectHandle(type, data){
				this.select(type, data)
			},
			toText(nodeConfig){
				if (nodeConfig.nodeUserList && nodeConfig.nodeUserList.length>0) {
					const users = nodeConfig.nodeUserList.map(item=>item.name).join("、")
					return users
				}else{
					if(nodeConfig.userSelectFlag){
						return "发起人自选"
					}else{
						return false
					}

				}
			}
		}
	}
</script>
